import 'package:json_annotation/json_annotation.dart';
import '../utils/timestamp_utils.dart'; // 导入工具类
part 'post_model.g.dart';

@JsonSerializable()
class Post{
  @JsonKey(name: 'userId')
  final int userId;
  @JsonKey(name: 'id')
  final int id;
  @JsonKey(name: 'title')
  final String title;
  @JsonKey(name:'body')
  final String body;
  // 添加额外字段
  // @JsonKey(name: 'createdAt', fromJson: TimestampUtils.fromJson, toJson: TimestampUtils.toJson)
  // final DateTime? createdAt;

  // @JsonKey(name: 'updatedAt', fromJson: TimestampUtils.fromJson, toJson: TimestampUtils.toJson)
  // final DateTime? updatedAt;

  @JsonKey(name: 'likeCount', defaultValue: 0)
  final int likeCount;

  @JsonKey(name: 'commentCount', defaultValue: 0)
  final int commentCount;

  Post({
    required this.userId,
    required this.id,
    required this.title,
    required this.body,
    // this.createdAt,
    // this.updatedAt,
    this.likeCount = 0,
    this.commentCount = 0,
  });
  // 简洁标题
  String get shortTitle {
    if (title.length <= 30) return title;
    return '${title.substring(0, 27)}...';
  }

  // 摘要
  String get summary {
    if (body.length <= 100) return body;
    return '${body.substring(0, 97)}...';
  }

  // 格式化创建时间
  // String get formattedCreatedAt => TimestampUtils.format(createdAt);
  //
  // // 相对时间
  // String get relativeTime => TimestampUtils.relativeTime(createdAt);
  //
  // // 是否是今天发布的
  // bool get isToday => TimestampUtils.isToday(createdAt);


  factory Post.fromJson(Map<String,dynamic> json)=> _$PostFromJson(json);
  Map<String,dynamic> toJson() => _$PostToJson(this);
}
